home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
util
/
cacheable.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
9KB
|
327 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
from __future__ import with_statement
__metaclass__ = type
from primitives import autoassign, Storage as S
from introspect import base_classes
import stdpaths
import httplib2
from logging import getLogger
log = getLogger('cacheable')
info = log.info
from os.path import split as pathsplit, exists as pathexists, join as pathjoin
import os
import sys
import traceback
from zlib import compress, decompress
import cPickle
_cacheattr = '__cached'
def save_cache_object(name, obj, user = False):
try:
f = _[2]
f.write(compress(cPickle.dumps(obj)))
finally:
pass
def load_cache_object(name, user = False):
p = get_cache_root(user) / name
if not p.exists():
return None
try:
f = _[2]
return cPickle.loads(decompress(f.read()))
finally:
pass
def get_cache_root(user = False):
root = stdpaths.userlocaldata / 'cache'
if user:
profile = profile
import common
if profile:
name = profile.username
else:
name = ''
root = root / (name + '_cache')
return root
def get_obj_cache_path(obj, user = False):
ocp = getattr(obj, 'cache_path', '')
if not ocp:
import warnings as warnings
warnings.warn('%r does not have a cache path, you really should give it one.' % obj)
return get_cache_root(user) / ocp
def clear_file_cache(obj, user = False):
return os.remove(get_obj_cache_path(obj, user = user))
def load_file_cache(obj, user = False):
cache_path = get_obj_cache_path(obj, user)
if not pathexists(cache_path):
return None
try:
f = _[2]
return f.read()
finally:
pass
def save_file_cache(obj, data, user = False):
cache_path = get_obj_cache_path(obj, user)
cache_head = pathsplit(cache_path)[0]
if not pathexists(cache_head):
os.makedirs(cache_head)
try:
f = _[2]
f.write(data)
finally:
pass
class cproperty:
def __init__(self, default = sentinel, box = None, unbox = None, user = False):
self.default = default
if box is not None:
self.box = box
self.unbox = unbox
else:
self.box = self.unbox = (lambda o: o)
self.usermode = user
def load_cache(self, obj):
data = load_file_cache(obj, self.usermode)
if hasattr(obj, 'cache_crypt'):
if data is not None:
try:
data = obj.cache_crypt[1](data)
except Exception:
e = None
traceback.print_exc()
print repr(data), repr(e)
except:
None<EXCEPTION MATCH>Exception
None<EXCEPTION MATCH>Exception
try:
boxed = None if data is not None else { }
except Exception:
e = None
traceback.print_exc()
boxed = { }
cache = { }
error = False
for k, v in boxed.iteritems():
try:
cache.update({
k: self.cacheprop(obj, k).unbox(v) })
continue
except Exception:
traceback.print_exc()
error = True
continue
if error:
log.warning('error retreiving cache for %s', obj)
setattr(obj, _cacheattr, cache)
return cache
def save_cache(self, obj):
cache = getattr(obj, _cacheattr)
boxed = (None, dict)((lambda .0: for k, v in .0:
(k, self.cacheprop(obj, k).box(v)))(cache.iteritems()))
compressed = compress(cPickle.dumps(boxed))
if hasattr(obj, 'cache_crypt'):
compressed = obj.cache_crypt[0](compressed)
save_file_cache(obj, compressed, self.usermode)
def __get__(self, obj, objtype):
cache = getattr(obj, _cacheattr, sentinel)
if cache is sentinel:
try:
cache = self.load_cache(obj)
except Exception:
traceback.print_exc()
msg = 'error when getting cached attribute %s of %r' % (self.name(objtype), obj)
print >>sys.stderr, msg
cache = { }
except:
None<EXCEPTION MATCH>Exception
None<EXCEPTION MATCH>Exception
name = self.name(objtype)
try:
val = cache[name]
except KeyError:
try:
val = self.default()
except (AttributeError, TypeError):
val = self.default
if val is not sentinel:
cache[name] = val
except:
val is not sentinel
if val is sentinel:
raise AttributeError
return val
def name(self, objtype):
try:
return self._name
except:
bases = [
objtype] + base_classes(objtype)
for clz in bases:
for k, v in clz.__dict__.iteritems():
if v is self:
self._name = k
return k
continue
raise AssertionError, str(bases)
def cacheprop(cls, objtype, name):
objtype = objtype.__class__
try:
return vars(objtype)[name]
except:
bases = base_classes(objtype)
for base in bases:
try:
return vars(base)[name]
continue
except KeyError:
continue
raise AssertionError, 'cannot find %s in %s' % (name, objtype)
cacheprop = classmethod(cacheprop)
def __set__(self, obj, value):
cache = getattr(obj, _cacheattr, sentinel)
if cache is sentinel:
try:
cache = self.load_cache(obj)
except Exception:
e = None
traceback.print_exc()
print >>sys.stderr, 'error loading cache for %r while setting value %s' % (obj, self.name(obj.__class__))
cache = { }
except:
None<EXCEPTION MATCH>Exception
None<EXCEPTION MATCH>Exception
cache[self.name(obj.__class__)] = value
try:
self.save_cache(obj)
except Exception:
e = None
traceback.print_exc()
print >>sys.stderr, 'error saving cache for %r while setting value %s' % (obj, self.name(obj.__class__))
def __delete__(self, obj):
self.fdel(obj)
class Buddy:
def __init__(self, name, protocol):
autoassign(self, locals())
def cache_path(self):
proto = self.protocol
return pathjoin(proto.name, proto.username, self.name) + '.dat'
cache_path = property(cache_path)
remote_alias = cproperty(5)
def urlcacheopen(url, *a, **k):
http = httplib2.Http(cache = unicode(get_cache_root() / 'webcache'))
(resp, content) = http.request(url, *a, **k)
log.debug('%s for %s', resp.status, url)
return (resp, content)
if __name__ == '__main__':
import wx
app = wx.PySimpleApp()
b = Buddy('Joe', S(name = 'aim', username = 'digsby01'))
print b.remote_alias
b.remote_alias = 'network alias'
class Foo(dict):
pass
class TestAble(object):
def cache_path(self):
return 'testable.dat'
cache_path = property(cache_path)
uids = cproperty(Foo, box = dict, unbox = Foo)
t = TestAble()
print t.uids
t.uids[0] = 'sdfasd'
print t.uids